//
//  TZFIndTableViewCell.m
//  XMLYFM
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "TZFIndTableViewCell.h"
#import "TZdetailCell.h"
#import "TZhotModel.h"
#import "TZeditorModel.h"
#import "TZHotHeader.h"

@interface TZFIndTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,retain)UICollectionView *myCollectionView;
@property(nonatomic,retain)NSMutableArray *allmodelarray;
@property(nonatomic,strong)NSMutableArray *nameArray;

@end
@implementation TZFIndTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self creatSubView];
        self.allmodelarray = [NSMutableArray array];
        self.nameArray = [NSMutableArray array];
    }

    return self;
    
}

-(void)creatSubView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 15;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    flowLayout.itemSize = CGSizeMake((TZWIDTH-40)/3,TZWIDTH *0.5);
    
    
    //设置头部区域大小
    flowLayout.headerReferenceSize = CGSizeMake(TZWIDTH, TZWIDTH * 0.11);
    flowLayout.footerReferenceSize = CGSizeMake(TZWIDTH, TZWIDTH *0.05);
    
    
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,TZWIDTH,TZWIDTH *0.61*13 + 60 +TZWIDTH *0.05*13) collectionViewLayout:flowLayout];
    self.myCollectionView.scrollEnabled = NO;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.myCollectionView];
    self.myCollectionView.showsHorizontalScrollIndicator = NO;
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
    
    //注册头部
    [self.myCollectionView registerClass:[TZHotHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    //注册尾部
    
    
    [self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    //注册cell
    [self.myCollectionView registerClass:[TZdetailCell class] forCellWithReuseIdentifier:@"TZdetailCell"];

    

}






//设置头部和底部区域内容
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    if (kind == UICollectionElementKindSectionHeader) {
        //创建头部区域view
        TZHotHeader *headView = [collectionView  dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//        headView.backgroundColor = [UIColor orangeColor];
     NSString *str = self.nameArray[indexPath.section];
        headView.titleLabel.text = str;
        headView.arrowImageView.image = [UIImage imageNamed:@"TZting.jpeg"];
        return headView;
        
    }

     else
   {
       kind = UICollectionElementKindSectionFooter;
       UICollectionReusableView *footr = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
       footr.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.91 alpha:1];
       return footr;
   }

}




//collectionView 协议方法


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return self.allmodelarray.count;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    
    TZhotModel *hotModel = [self.allmodelarray objectAtIndex:section];

    return hotModel.list.count;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{
    
    NSMutableArray *tempArray = [NSMutableArray array];
    TZhotModel *model = [self.allmodelarray objectAtIndex:indexPath.section];
    
    
    static NSString *cellId = @"TZdetailCell";
    
    TZdetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
    NSMutableArray *Array  =  model.list;
  
    
    for (NSMutableDictionary *dic in Array) {
        TZeditorModel *model = [[TZeditorModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [tempArray addObject:model];
       
    }
  
   
    TZeditorModel *mymodel = [tempArray objectAtIndex:indexPath.row];
//     NSLog(@"🐷🐷🐷5555%@",mymodel.title);
    
    
    cell.Model = mymodel;
    cell.backgroundColor = [UIColor purpleColor];
    

    return cell;
    
}

//重写字典 给数组赋值
-(void)setMyDic:(NSMutableDictionary *)myDic
{
    
    [self.allmodelarray removeAllObjects];
    
    //    NSLog(@"1111111%@",myDic);
    NSMutableArray *Array = myDic[@"list"];
   
    for (NSMutableDictionary *dic in Array) {
        TZhotModel *hotModel = [[TZhotModel alloc]init];
        [hotModel setValuesForKeysWithDictionary:dic];
//        NSLog(@"sdf%@",hotModel.title);
        [self.allmodelarray addObject:hotModel];
        [self.nameArray addObject:hotModel.title];
        
    }
//     NSLog(@"🐶🐶🐶55555%ld",self.allmodelarray.count);
    
    [self.myCollectionView reloadData];
    


}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{  NSMutableArray *temp= [NSMutableArray array];
    TZhotModel *hotModel = self.allmodelarray[indexPath.section];
    NSMutableArray *Array =  hotModel.list;
    
    for (NSMutableDictionary *dic in Array) {
        TZeditorModel *detail = [[TZeditorModel alloc]init];
        [detail setValuesForKeysWithDictionary:dic];
        [temp addObject:detail];
    }
    TZeditorModel *model = temp[indexPath.row];
    
    [self.picTableCellDelegate JumpToAlbumView:model.albumId :model.title :model.trackId :model];

    

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
